package com.matrix.common.enums;

import com.baomidou.mybatisplus.annotation.EnumValue;
import com.fasterxml.jackson.annotation.JsonValue;
import lombok.Getter;

/**
 * @author 有点甜
 * @since 2025/8/1
 */
@Getter
public enum OrderStatusEnum {
    PROCESSING(5, "处理中"),
    PENDING_PAYMENT(10, "待支付"),
    PENDING_ACCEPTANCE(20, "待接单"),
    IN_PROGRESS(30, "制作中"),
    WAITING_FOR_SERVING(40, "待上菜"),
    SERVING(50, "上菜中"),
    SERVING_COMPLETED(60, "上菜完毕(用餐中)"),
    PENDING_REVIEW(70, "待评价"),
    CANCELLED(80, "已取消"),
    COMPLETED(90, "已完成(已评价)"),
    CREATE_FAILED(100, "创建失败");

    @EnumValue
    private final int code;

    @JsonValue
    private final String description;

    OrderStatusEnum(int code, String description) {
        this.code = code;
        this.description = description;
    }

    public static OrderStatusEnum fromCode(int code) {
        for (OrderStatusEnum status : OrderStatusEnum.values()) {
            if (status.getCode() == code) {
                return status;
            }
        }
        throw new IllegalArgumentException("Unknown order status code: " + code);
    }

    public boolean canAccept() {
        return this == PENDING_ACCEPTANCE;
    }

    public boolean canFinishProduction() {
        return this == IN_PROGRESS;
    }

    public boolean canStartServing() {
        return this == WAITING_FOR_SERVING;
    }

    public boolean canFinishServing() {
        return this == WAITING_FOR_SERVING || this == SERVING;
    }

    public boolean canFinishMeal() {
        return this == SERVING_COMPLETED;
    }

    public boolean canReview() {
        return this == PENDING_REVIEW;
    }

    public boolean canAddOrCancelDish() {
        return this == IN_PROGRESS || this == WAITING_FOR_SERVING || this == SERVING || this == SERVING_COMPLETED;
    }

    public String getCannotAddOrCancelDishMessage() {
        return switch (this) {
            case PENDING_PAYMENT -> "订单尚未支付,无法操作";
            case PENDING_ACCEPTANCE -> "订单等待商家接单中,请稍后再试";
            case PENDING_REVIEW, COMPLETED -> "用餐已结束,无法操作";
            case CANCELLED -> "订单已取消,无法操作";
            default -> "当前订单状态不支持此操作";
        };
    }
}
